import { defineStore } from 'pinia'
import { ref } from 'vue'
import { activityApi } from '@/api/activity'

export const useActivityStore = defineStore('activity', () => {
  const activities = ref([])
  const currentActivity = ref(null)
  const loading = ref(false)
  const error = ref(null)

  // 获取活动列表
  const getActivityList = async (params = {}) => {
    loading.value = true
    error.value = null
    
    try {
      const response = await activityApi.getActivityList(params)
      return response.data || { list: [] }
    } catch (err) {
      console.error('获取活动列表失败:', err)
      error.value = err.message || '获取活动列表失败'
      return { list: [] }
    } finally {
      loading.value = false
    }
  }

  // 获取活动详情
  const fetchActivityDetail = async (id) => {
    loading.value = true
    error.value = null
    
    try {
      const response = await activityApi.getActivityDetail(id)
      currentActivity.value = response.data
      return response
    } catch (err) {
      console.error('获取活动详情失败:', err)
      error.value = err.message || '获取活动详情失败'
      throw err
    } finally {
      loading.value = false
    }
  }

  // 收藏活动
  const favoriteActivity = async (id) => {
    try {
      const response = await activityApi.favoriteActivity(id)
      return response.data
    } catch (err) {
      console.error('收藏活动失败:', err)
      throw err
    }
  }

  // 取消收藏活动
  const unfavoriteActivity = async (id) => {
    try {
      const response = await activityApi.unfavoriteActivity(id)
      return response.data
    } catch (err) {
      console.error('取消收藏活动失败:', err)
      throw err
    }
  }

  // 检查是否已收藏
  const checkFavorite = async (id) => {
    try {
      const response = await activityApi.checkFavorite(id)
      return response.data?.isFavorite || false
    } catch (err) {
      console.error('检查收藏状态失败:', err)
      return false
    }
  }

  // 报名参加活动
  const joinActivity = async (id) => {
    try {
      const response = await activityApi.joinActivity(id)
      return response.data
    } catch (err) {
      console.error('报名活动失败:', err)
      throw err
    }
  }

  // 检查报名状态
  const checkJoinStatus = async (id) => {
    try {
      const response = await activityApi.checkJoinStatus(id)
      return response.data?.isJoined || false
    } catch (err) {
      console.error('检查报名状态失败:', err)
      return false
    }
  }

  // 获取收藏的活动列表
  const getFavoriteActivities = async (page = 1, pageSize = 10) => {
    try {
      const response = await activityApi.getFavoriteActivities(page, pageSize)
      return response.data || { list: [] }
    } catch (err) {
      console.error('获取收藏活动失败:', err)
      return { list: [] }
    }
  }

  // 创建活动
  const createActivity = async (activityData) => {
    try {
      const response = await activityApi.createActivity(activityData)
      return response.data
    } catch (err) {
      console.error('创建活动失败:', err)
      throw err
    }
  }

  return {
    activities,
    currentActivity,
    loading,
    error,
    getActivityList,
    fetchActivityDetail,
    favoriteActivity,
    unfavoriteActivity,
    checkFavorite,
    joinActivity,
    checkJoinStatus,
    getFavoriteActivities,
    createActivity,
  }
})






